package com.connor.bs.plm;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentGroup;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCComponentUserType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCProperty;
import com.teamcenter.rac.kernel.TCSession;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class SampleHandler extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public SampleHandler() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		try {
			FileInputStream fis=new FileInputStream(new File("C:\\change.xlsx"));
			XSSFWorkbook workbook=new XSSFWorkbook(fis);
			XSSFSheet sheet=workbook.getSheetAt(0);
			XSSFRow row;
			XSSFCell cell;
			int rowNum=sheet.getPhysicalNumberOfRows();
			TCSession session=(TCSession)AIFUtility.getCurrentApplication().getSession();
			TCComponentItemType itemType=(TCComponentItemType) session.getTypeComponent("Item");
			TCComponentItem item;
			TCComponentUserType userType=(TCComponentUserType) session.getTypeComponent("User");
			TCComponentUser user;
			TCComponentGroup group;
			TCComponentGroup[] groups;
			TCComponentItem[] items;
			String userName;
			String id;
			String groupName;
			for(int i=1;i<rowNum;i++){
				row=sheet.getRow(i);
				cell=row.getCell(0);
				id=cell.getStringCellValue();
				items=itemType.findItems(id);
				if(items.length==0)
					continue;
				System.out.println("id:"+id);
				item=items[0];
				System.out.println("index:"+i);
				if(item!=null){
					group=null;
					cell=row.getCell(1);
					userName=cell.getStringCellValue();
					System.out.println("userName:"+userName);
					user=userType.find(cell.getStringCellValue());
					if(user!=null){
						groups=user.getGroups();
						groupName=row.getCell(2).getStringCellValue();
						for(int j=0;j<groups.length;j++){
							System.out.println("groupName:"+groups[j].getGroupName());
							if(groups[j].getGroupName().equals(groupName)){
								group=groups[j];
							}
							groups[j].getGroups();
						}
						if(group!=null){
							changeOnwer(item, user, group);
							AIFComponentContext[] contexts=item.getChildren();
							for(int k=0;k<contexts.length;k++) {
								if(contexts[k].getComponent() instanceof TCComponentItemRevision)
									changeOnwer((TCComponent)contexts[k].getComponent(), user, group);
							}
						}else{
							System.out.println("not exist group:"+groupName);
						}
					}else{
						System.out.println("not exist user:"+userName);
					}
				}else{
					System.out.println("not exist id:"+id);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	private void changeOnwer(TCComponent comp,TCComponentUser user,TCComponentGroup group){
		
		try {
			comp.changeOwner(user, group);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		AIFComponentContext[] context;
		try {
			context = comp.getChildren();
			for(int i=0;i<context.length;i++){
				if((context[i].getComponent() instanceof TCComponentItemRevision)||(context[i].getComponent() instanceof TCComponentItem))
					continue;
				changeOnwer((TCComponent)context[i].getComponent(), user, group);
			}
		} catch (TCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
